#include "CollectChannlForm.h"
#include "ui_CollectChannlForm.h"
#include "toObject/ModelXml.h"
CollectChannlForm::CollectChannlForm(QWidget *parent) :
    AttribBaseForm(parent),
    ui(new Ui::CollectChannlForm)
{
    ui->setupUi(this);
    QWidget * widgetInvented = new QWidget();
    widgetInvented = new QWidget();
    QHBoxLayout * hLayout4 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("备用参数");
        QLineEdit * edit1 = new QLineEdit();
        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        m_mapChilds.insert("reserve",edit1);
    }
    widgetInvented->setLayout(hLayout4);
    ui->stackedWidget->addWidget(widgetInvented);

    QWidget * widgetSerial = new QWidget();
    QHBoxLayout * hLayout2 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("串口号");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("comName",edit1);
        QLabel * label2 = new QLabel("波特率");
        QComboBox * combox = new QComboBox();
        combox->addItems(QStringList{"4800","9600","14400","19200","115200"});
        combox->setCurrentIndex(1);
        combox->setEditable(true);
         m_mapChilds.insert("baudRate",combox);
        QLabel * label3 = new QLabel("数据位");
        QComboBox * combox1 = new QComboBox();
        combox1->addItems(QStringList{"4","5","6","7","8"});
        combox1->setCurrentIndex(3);
        m_mapChilds.insert("dataBit",combox1);
        QLabel * label4 = new QLabel("检验位");
        QComboBox * combox2 = new QComboBox();
        combox2->addItems(QStringList{"奇","偶","无"});
        m_mapChilds.insert("checkBit",combox2);
        QLabel * label5 = new QLabel("停止位");
        QComboBox * combox3 = new QComboBox();
        combox3->addItems(QStringList{"1","1.5","2"});
        m_mapChilds.insert("stopBit",combox3);
        hLayout2->addWidget(label1);
        hLayout2->addWidget(edit1);
        hLayout2->addWidget(label2);
        hLayout2->addWidget(combox);
        hLayout2->addWidget(label3);
        hLayout2->addWidget(combox1 );
        hLayout2->addWidget(label4 );
        hLayout2->addWidget(combox2 );
        hLayout2->addWidget(label5 );
        hLayout2->addWidget(combox3 );
    }
    hLayout2->setMargin(0);
    widgetSerial->setLayout(hLayout2);
    ui->stackedWidget->addWidget(widgetSerial);

    connectAllEdit();
}

CollectChannlForm::~CollectChannlForm()
{
    delete ui;
}

void CollectChannlForm::InitUIData()
{
    m_initAfter = false;
    stCollectChannel stChannel ;
    stChannel = ModelXml::GetInstance()->GetCollectChannel(m_channelID);
    if(stChannel.id == 0 ) return ;

    ui->lineEdit->setText(stChannel.name.c_str());
    ui->lineEdit_2->setText(stChannel.desc.c_str());
    ui->lineEdit_3->setText(stChannel.protocate.c_str());
    ui->lineEdit_4->setText(stChannel.protoName.c_str());

    ui->comboBox->setCurrentIndex(stChannel.portType);
    ((QLineEdit *) m_mapChilds["reserve"])->setText(stChannel.reserve.c_str());
    ((QLineEdit *) m_mapChilds["comName"])->setText(stChannel.serialport.name.c_str());
    ((QComboBox *) m_mapChilds["baudRate"])->setCurrentText(stChannel.serialport.baudRate.c_str());
    ((QComboBox *) m_mapChilds["dataBit"])->setCurrentText(stChannel.serialport.dataBit.c_str());
    ((QComboBox *) m_mapChilds["checkBit"])->setCurrentText(stChannel.serialport.checkBit.c_str());
    ((QComboBox *) m_mapChilds["stopBit"])->setCurrentText(stChannel.serialport.stopBit.c_str());
}

void CollectChannlForm::saveModel()
{
    stCollectChannel stChannel = GetUIData();
    ui->lbSavePrompt->setText("");
    m_currentTreeItem->setText(0,ui->lineEdit->text()+ ":"+ ui->lineEdit_4->text().toUpper());
    ModelXml::GetInstance()->ModiCollectChannel(stChannel);
    ModelXml::GetInstance()->SaveXml();
}

stCollectChannel CollectChannlForm::GetUIData()
{
    stCollectChannel stChannel ;
    stChannel.name = ui->lineEdit->text().toStdString();
    stChannel.desc = ui->lineEdit_2->text().toStdString();
    stChannel.protocate = ui->lineEdit_3->text().toStdString();
    stChannel.protoName = ui->lineEdit_4->text().toStdString();
    stChannel.portType = (stCollectChannel::ChannelPortType)ui->comboBox->currentIndex();
    stChannel.reserve = ((QLineEdit *) m_mapChilds["reserve"])->text().toStdString();
    stChannel.serialport.name = ((QLineEdit *) m_mapChilds["comName"])->text().toStdString();
    stChannel.serialport.dataBit = ((QComboBox *) m_mapChilds["dataBit"])->currentText().toStdString();
    stChannel.serialport.stopBit = ((QComboBox *) m_mapChilds["stopBit"])->currentText().toStdString();
    stChannel.serialport.baudRate = ((QComboBox *) m_mapChilds["baudRate"])->currentText().toStdString();
    stChannel.serialport.checkBit = ((QComboBox *) m_mapChilds["checkBit"])->currentText().toStdString();
    stChannel.id = m_channelID;
    return stChannel;
}

void CollectChannlForm::on_save_Button_clicked()
{
    GetUIData();
    ui->lbSavePrompt->setText("");
    m_currentTreeItem->setText(0,ui->lineEdit->text()+ ":"+ ui->lineEdit_4->text().toUpper());
    ModelXml::GetInstance()->SaveXml();
}

void CollectChannlForm::on_comboBox_currentIndexChanged(int index)
{
  //  if(index == 0)
    ui->stackedWidget->setCurrentIndex(index);
}

void CollectChannlForm::on_textChanged(const QString &)
{
    if(m_initAfter)
        ui->lbSavePrompt->setText("属性已发生变化，请保存=>");
}
